import matplotlib.pyplot as plt
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QIcon

class Activation(object):
    def __init__(self, start, end, id):
        self.start = start
        self.end = end
        self.select = False
        self.id = id


class Ui_mainWindow(object):
    def setupUi(self, mainWindow):
        mainWindow.setObjectName("mainWindow")
        mainWindow.setWindowModality(QtCore.Qt.WindowModal)
        mainWindow.resize(624, 511)
        # mainWindow.setFixedSize(841,556)
        self.centralWidget = QtWidgets.QWidget(mainWindow)
        self.centralWidget.setObjectName("centralWidget")

        self.centralWidget = QtWidgets.QWidget(mainWindow)
        self.centralWidget.setObjectName("centralWidget")
        self.lineEdit = QtWidgets.QLineEdit(self.centralWidget)
        self.lineEdit.setGeometry(QtCore.QRect(210, 70, 181, 111))
        self.lineEdit.setFrame(False)

        self.lineEdit.setClearButtonEnabled(True)
        self.lineEdit.setObjectName("lineEdit")
        self.lineEdit.setText("一个数据小白菜")
        self.lineEdit.setStyleSheet("color:red")
        font = QtGui.QFont()
        font.setFamily('微软雅黑')
        font.setBold(True)
        font.setPointSize(10)
        font.setWeight(75)
        self.lineEdit.setFont(font)
        self.lineEdit.setAlignment(QtCore.Qt.AlignCenter)
        mainWindow.setCentralWidget(self.centralWidget)

        mainWindow.setCentralWidget(self.centralWidget)
        self.retranslateUi(mainWindow)
        QtCore.QMetaObject.connectSlotsByName(mainWindow)
    def retranslateUi(self, mainWindow):
        _translate = QtCore.QCoreApplication.translate
        mainWindow.setWindowTitle('相控阵雷达资源调度优化算法')
        mainWindow.setWindowIcon(QIcon('logo.png'))

# 按照活动的结束时间增序对活动整体排序，挑选活动时，活动的结束最早的优先被选取，为剩下活动留下最多的时间来安排
# 每次使剩下的时间可能安排最多的活动，或说是每次自己占用的时间资源最少

def main():
    act_num = eval(input("请输入活动总的个数："))
    act_list = []
    for i in range(act_num):
        a = eval(input("请输入第{}个活动起始时间：".format(i + 1)))
        b = eval(input("请输入第{}个活动结束时间：".format(i + 1)))
        if a >= b:
            print("error:结束时间小于等于了开始")
            b = eval(input("请重新输入第{}个活动结束时间：".format(i + 1)))
        act_list.append(Activation(a, b, i + 1))
    act_list.sort(key=lambda obj: obj.end, reverse=False)
    last = 0
    for i in range(act_num):
        if i == 0:
            act_list[i].select = True
        elif act_list[i].start >= act_list[last].end:
            act_list[i].select = True
            last = i
    # 可视化（结果）
    fig, ax = plt.subplots()
    order = 1  # 行数
    for i in act_list:
        if i.select == True:
            ax.broken_barh([(i.start, i.end - i.start)], (10 * order, 9), facecolor='green')
        else:
            ax.broken_barh([(i.start, i.end - i.start)], (10 * order, 9), facecolor='blue')
        order = order + 1
    ax.set_ylim(5, 10 * act_num + 15)
    # 自适应大小
    ax.set_xlim(0, act_list[-1].end)
    ax.set_xlabel('time order')
    act_y = []
    act_lebal = []
    for i in range(act_num):
        act_y.append(10 * i + 15)
        act_lebal.append("activity{}".format(act_list[i].id))
    ax.set_yticks(act_y)
    ax.set_yticklabels(act_lebal)
    ax.grid(True)
    ax.annotate('race interrupted', (61, 25),
                xytext=(0.8, 0.9), textcoords='axes fraction',
                arrowprops=dict(facecolor='black', shrink=0.05),
                fontsize=16,
                horizontalalignment='right', verticalalignment='top')

    plt.show()


main()